﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using kp.koordinator.contract;
using kp.plotter.contract;

namespace kp.plotter
{

    public partial class Portal : Form, WinFormsHost.IEntryPoint
    {
        private readonly kp.koordinator.contract.IKoordinator _koordinator;
        private readonly kp.plotter.contract.IPlotter _plotter;
       

        public Portal( IKoordinator koordinator,  IPlotter plotter)
        {
            _koordinator = koordinator;
            _plotter = plotter;
            InitializeComponent();

            var plotterUserControl = (UserControl) plotter;
            this.Controls.Add( plotterUserControl);
            Portal_Resize(null, null); 
        }

        #region IEntryPoint Members

        public System.Windows.Forms.Form MainForm
        {
            get { return this; }
        }

        #endregion

        private void buttonPlot_Click(object sender, EventArgs e)
        {
            double min = double.Parse(this.textBoxMin.Text);
            double max = double.Parse(this.textBoxMax.Text);
            int steps = _plotter.Werteanzahl;
            string formula = this.textBoxFormula.Text;

            PlotIt(formula, min, max, steps);
        }

        private void PlotIt(string formula, double min, double max, int steps)
        {
            var result = this._koordinator.Berechne(formula, min, max, steps);
            this._plotter.ShowPlot( result );
        }

        private void Portal_Load(object sender, EventArgs e)
        {

        }

        private void Portal_Resize(object sender, EventArgs e)
        {
            var plotterUserControl = (UserControl)_plotter;
            buttonPlotDummy.Visible = true;
            plotterUserControl.Location = new Point( buttonPlotDummy.Location.X, buttonPlotDummy.Location.Y );
            plotterUserControl.Size = new Size( buttonPlotDummy.Size.Width, buttonPlotDummy.Size.Height);

            plotterUserControl.Refresh();
            buttonPlotDummy.Visible = false;
 
        }
    }

}
